//
//  GS_EffectivenessViewController.m
//  GoodManNews
//
//  Created by dllo on 16/1/16.
//  Copyright © 2016年 dllo. All rights reserved.
//

#define URL1(name, server) [NSString stringWithFormat:@"http://115.29.143.211/lol/apiMobile.php?action=getPlayersInfo&target=%@&serverName=%@", name, server]

#import "GS_EffectivenessViewController.h"
#import "GS_EffectivenessDetailsViewController.h"
#import "GoodManNews_AFNetTool.h"
#import <MBProgressHUD.h>

@interface GS_EffectivenessViewController () <UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate>

@property (nonatomic, retain) UIButton *confirmBut;
@property (nonatomic, retain) UITextField *textField;
@property (nonatomic, retain) UIButton *secondBut;
@property (nonatomic, retain) UIPickerView *allView;
@property (nonatomic, retain) NSArray *serverArr;
@property (nonatomic, retain) UIView *mainView;
@property (nonatomic, retain) UIView *blackView;
@property (nonatomic, retain) UIButton *returnBut;
@property (nonatomic, retain) UIButton *okBut;
@property (nonatomic, retain) UILabel *midLabel;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *server;
@property (nonatomic, retain) UIImageView *imageV;
@property (nonatomic, copy) NSString *str;
@property (nonatomic, assign) NSInteger num;
@property (nonatomic, retain) MBProgressHUD *hud;

@end

@implementation GS_EffectivenessViewController

- (void)dealloc
{
    [_textField release];
    [_allView release];
    [_serverArr release];
    [_mainView release];
    [_blackView release];
    [_midLabel release];
    [_name release];
    [_server release];
    [_imageV release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"战斗力查询";
    self.view.backgroundColor = [UIColor orangeColor];
    self.imageV = [UIImageView new];
    self.imageV.image = [UIImage imageNamed:@"background.jpg"];
    [self.view addSubview:self.imageV];
    [self.imageV mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.edges.equalTo(self.view);
        
    }];
     self.num = 0;
    [self createViews];
    self.server = @"电信一 艾欧尼亚";
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}

- (void)createViews
{
    self.blackView = [UIView new];
    self.blackView.backgroundColor = [UIColor blackColor];
    self.blackView.alpha = 0.5;
    [self.view addSubview:self.blackView];
    
    [self.blackView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.edges.equalTo(self.view);
        
    }];

    self.blackView.hidden = YES;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    self.blackView.userInteractionEnabled = YES;
    [self.blackView addGestureRecognizer:tap];
    [tap release];
    
    
    UIView *cellerV = [UIView new];
    cellerV.backgroundColor = [UIColor whiteColor];
    cellerV.layer.cornerRadius = 5;
    cellerV.layer.masksToBounds = YES;
    [self.view addSubview:cellerV];
    
    UIView *serverV = [UIView new];
    serverV.backgroundColor = [UIColor whiteColor];
    serverV.layer.cornerRadius = 5;
    serverV.layer.masksToBounds = YES;
    [self.view addSubview:serverV];
    
    [cellerV mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.equalTo(self.view);
        make.centerY.equalTo(self.view).with.offset(-150);
        make.width.equalTo(@260);
        make.height.equalTo(@40);
        
    }];
    
    [serverV mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.equalTo(self.view);
        make.centerY.equalTo(self.view).with.offset(-90);
        make.width.equalTo(@260);
        make.height.equalTo(@40);
        
    }];
    
    self.confirmBut = [UIButton buttonWithType:UIButtonTypeCustom];
    self.confirmBut.backgroundColor = [UIColor orangeColor];
    [self.confirmBut setTitle:@"查询" forState:UIControlStateNormal];
    self.confirmBut.layer.cornerRadius = 5;
    self.confirmBut.layer.masksToBounds = YES;
    [self.confirmBut addTarget:self action:@selector(confirmButAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.confirmBut];
    
    [self.confirmBut mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.equalTo(self.view);
        make.centerY.equalTo(self.view).with.offset(-30);
        make.width.equalTo(@260);
        make.height.equalTo(@40);
        
    }];
    
    UILabel *callerL = [UILabel new];
    callerL.backgroundColor = [UIColor whiteColor];
    callerL.textColor = [UIColor blackColor];
    callerL.text = @"召唤师:";
    callerL.font = [UIFont systemFontOfSize:14];
    [cellerV addSubview:callerL];

    [callerL mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(cellerV.mas_left).with.offset(10);
        make.top.equalTo(cellerV.mas_top).with.offset(10);
        make.bottom.equalTo(cellerV.mas_bottom).with.offset(-10);
        make.width.equalTo(@50);
        
        
    }];
    
    self.textField = [UITextField new];
    self.textField.placeholder = @"请输入召唤师名字";
    self.textField.clearButtonMode = UITextFieldViewModeAlways;
    self.textField.delegate = self;
//    self.textField.backgroundColor = [UIColor purpleColor];
    self.textField.font = [UIFont systemFontOfSize:14];
    [cellerV addSubview:self.textField];
    
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(@70);
        make.top.equalTo(callerL);
        make.bottom.equalTo(callerL);
        make.right.equalTo(cellerV.mas_right).with.offset(-10);
        
    }];
    
    self.secondBut = [UIButton buttonWithType:UIButtonTypeCustom];
//    self.secondBut.backgroundColor = [UIColor blackColor];
    [self.secondBut setTitle:@"请选择服务器" forState:UIControlStateNormal];
    self.secondBut.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.secondBut addTarget:self action:@selector(secondButAction) forControlEvents:UIControlEventTouchUpInside];
    [self.secondBut setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [serverV addSubview:self.secondBut];
    
    [self.secondBut mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(serverV).with.offset(10);
        make.top.equalTo(serverV).with.offset(10);
        make.bottom.equalTo(serverV.mas_bottom).with.offset(-10);
        make.right.equalTo(serverV.mas_right).with.offset(-10);
        
    }];
    
    self.mainView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height + 240, self.view.bounds.size.width, 240)];
    self.mainView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:self.mainView];
    [_mainView release];
    
    //创建返回按钮
    self.returnBut = [UIButton buttonWithType:UIButtonTypeCustom];
//    self.returnBut.backgroundColor = [UIColor greenColor];
    [self.returnBut setTitle:@"返回" forState:UIControlStateNormal];
    [self.returnBut setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.returnBut addTarget:self action:@selector(returnButAction) forControlEvents:UIControlEventTouchUpInside];
    self.returnBut.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.mainView addSubview:self.returnBut];
    
    //创建中间lebel
    self.midLabel = [UILabel new];
//    self.midLabel.backgroundColor = [UIColor purpleColor];
    self.midLabel.text = @"选择服务器";
    self.midLabel.font = [UIFont systemFontOfSize:13];
    self.midLabel.textAlignment = NSTextAlignmentCenter;
    [self.mainView addSubview:self.midLabel];
    
    //创建确定按钮
    self.okBut = [UIButton buttonWithType:UIButtonTypeCustom];
//    self.okBut.backgroundColor = [UIColor greenColor];
    [self.okBut setTitle:@"确认" forState:UIControlStateNormal];
    [self.okBut setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.okBut addTarget:self action:@selector(okButAction) forControlEvents:UIControlEventTouchUpInside];
    self.okBut.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.mainView addSubview:self.okBut];
    
    [self.returnBut mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(self.mainView);
        make.top.equalTo(self.mainView);
        make.right.equalTo(self.midLabel.mas_left);
        make.height.equalTo(@40);
        make.width.equalTo(@60);
        
    }];
    
    [self.midLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(self.returnBut.mas_right);
        make.top.equalTo(self.mainView);
        make.right.equalTo(self.okBut.mas_left);
        make.height.equalTo(@40);
        
    }];
    
    [self.okBut mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.mainView);
        make.right.equalTo(self.mainView.mas_right);
        make.height.equalTo(@40);
        make.width.equalTo(@60);
        
    }];
    
    
    self.allView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, self.view.bounds.size.width, 200)];
    self.allView.backgroundColor = [UIColor whiteColor];
    // 高亮背景显示标记
    self.allView.showsSelectionIndicator = YES;
    self.allView.delegate = self;
    self.allView.dataSource = self;
    self.serverArr = [NSArray arrayWithObjects:@"电信一 艾欧尼亚", @"电信二 祖安", @"电信三 诺克萨斯", @"电信四 班德尔城", @"电信五 皮尔特沃夫", @"电信六 战争学院", @"电信七 巨神峰", @"电信八 雷瑟守卫", @"电信九 裁决之地", @"电信十 黑色玫瑰", @"电信十一 暗影岛", @"电信十二 钢铁烈阳", @"电信十三 均衡教派", @"电信十四 水晶之痕", @"电信十五 影流", @"电信十六 守望之海", @"电信十七 征服之海", @"电信十八 卡拉曼达", @"电信十九 皮城警备", @"网通一 比尔吉沃特", @"网通二 德玛西亚", @"网通三 弗雷尔卓德", @"网通四 无畏先锋", @"网通五 恕瑞玛", @"网通六 扭曲丛林", @"网通七 巨龙之巢", @"教育一 教育网专区", nil];

    [self.mainView addSubview:self.allView];
    
    //添加触摸手势，当点击View上其他地方时会隐藏键盘
    UITapGestureRecognizer *tapRes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(resignResponders)];
    //将触摸事件添加到当前viewz
//    self.view.userInteractionEnabled = YES;
    [self.view addGestureRecognizer:tapRes];
    
    
}

- (void)confirmButAction
{
    self.hud = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:self.hud];
    self.hud.labelText = @"Loading";
    [self.hud show:YES];
//    NSLog(@"dianji");
    [self getData];
    
}

- (void)okButAction
{
    [UIView animateWithDuration:0.5 animations:^{
        
        self.mainView.frame = CGRectMake(0, self.view.bounds.size.height + 240, self.view.bounds.size.width, 240);
        
    }];
    self.blackView.hidden = YES;
    [self.secondBut setTitle:self.server forState:UIControlStateNormal];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.textField resignFirstResponder];
    return YES;
}

- (void)resignResponders {
    
    [self.textField resignFirstResponder];
}

- (void)returnButAction
{
    [UIView animateWithDuration:0.5 animations:^{
        
        self.mainView.frame = CGRectMake(0, self.view.bounds.size.height + 240, self.view.bounds.size.width, 240);
        
    }];
    self.blackView.hidden = YES;
    
}

//黑色view手势
- (void)tapAction:(UITapGestureRecognizer *)tap
{
    [UIView animateWithDuration:0.5 animations:^{
        
        self.mainView.frame = CGRectMake(0, self.view.bounds.size.height + 240, self.view.bounds.size.width, 240);
        
    }];
    self.blackView.hidden = YES;
    
}

// UIPickerViewDataSource协议中必须实现的方法 设置列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

// UIPickerViewDataSource协议中必须实现的方法 设置每列个数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    
    return self.serverArr.count;
    
}

// 设置每列宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {

        return 240;
}

// 返回选中的行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
//    NSLog(@"%@", self.serverArr[row]);
    self.server = self.serverArr[row];
}

//返回当前行的内容,此处是将数组中数值添加到滚动的那个显示栏上
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
        return [self.serverArr objectAtIndex:row];
}

- (void)secondButAction
{
    [self.textField resignFirstResponder];
    [UIView animateWithDuration:0.5 animations:^{
        
        self.mainView.frame = CGRectMake(0, self.view.bounds.size.height - 240, self.view.bounds.size.width, 240);
        
    }];
    self.blackView.hidden = NO;
    [self.view bringSubviewToFront:self.blackView];
    [self.view bringSubviewToFront:self.mainView];
    
}

- (void)getData
{
    NSArray *arr = [self.server componentsSeparatedByString:@" "];
    self.str = arr[0];
    self.name = self.textField.text;
//   NSLog(@"%@", URL1(self.name, self.str)) ;
    [GoodManNews_AFNetTool getNetWithURL:URL1(self.name, self.str) body:nil headFile:nil responseStyle:WBNJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        
        [self.hud hide:YES];
        
        if (responseObject) {
            GS_EffectivenessDetailsViewController *effVC = [[GS_EffectivenessDetailsViewController alloc] init];
            effVC.server = arr[1];
            
            effVC.servserT = arr[0];
            effVC.name = self.name;
            [self.navigationController pushViewController:effVC animated:YES];
            [effVC release];

        }
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        NSLog(@"++++++");
        
    }];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
